#include "app_sys.h"
#include "elog.h"
#include "bsp_radio_tx.h"

static char *TAG = "app_sys.c";

app_sys_para_t app_sys_para = {0};

/* 系统参数初始化 */
void app_sys_para_init(void)
{
    app_sys_para.tx_freq = 2450;
}

/* 发射频率设置 */
static void app_sys_set_tx_freq(uint16_t tx_freq)
{
    static uint16_t freq_old = 0xff;

    if (freq_old == tx_freq)
    {
        return;
    }

    freq_old = tx_freq;

    bsp_radio_tx_freq_set(tx_freq);
}

void app_sys_get_local_net_addr(uint8_t *addr1, uint8_t *addr2, uint8_t *addr3, uint8_t *addr4)
{
    *addr1 = 192;
    *addr2 = 168;
    *addr3 = 86;
    // *addr4 = 101;
    *addr4 = ETH_ADDR_AMC;
    // *addr4 = (uint8_t)app_sys_info.module.dev_addr;
    log_i("%s: local ip addr: %d.%d.%d.%d", TAG, *addr1, *addr2, *addr3, *addr4);
}

void app_sys_get_local_net_mac(uint8_t *addr1, uint8_t *addr2, uint8_t *addr3, uint8_t *addr4, uint8_t *addr5, uint8_t *addr6)
{
    *addr1 = 0x00;
    *addr2 = 0x80;
    *addr3 = 0xE1;
    *addr4 = 0x00;
    *addr5 = 0x00;
    // *addr6 = 0x01;
    *addr6 = ETH_ADDR_AMC;
    // *addr6 = (uint8_t)app_sys_info.module.dev_addr;
    log_i("%s: local mac addr: %d:%d:%d:%d:%d:%d", TAG, *addr1, *addr2, *addr3, *addr4, *addr5, *addr6);
}


void app_sys_log_init(void)
{
    /* initialize EasyLogger */
    elog_init();
    /* set EasyLogger log format */
    elog_set_fmt(ELOG_LVL_ASSERT, ELOG_FMT_LVL | ELOG_FMT_TIME);
    elog_set_fmt(ELOG_LVL_ERROR, ELOG_FMT_LVL | ELOG_FMT_TIME);
    elog_set_fmt(ELOG_LVL_WARN, ELOG_FMT_LVL | ELOG_FMT_TIME);
    elog_set_fmt(ELOG_LVL_INFO, ELOG_FMT_LVL | ELOG_FMT_TIME);
    elog_set_fmt(ELOG_LVL_DEBUG, ELOG_FMT_LVL | ELOG_FMT_TIME);
    elog_set_fmt(ELOG_LVL_VERBOSE, ELOG_FMT_LVL | ELOG_FMT_TIME);
    /* start EasyLogger */
    elog_start();
}

/* 系统任务 */
void app_sys_task(void)
{
    while (1)
    {
        /* 设置发射频率 */
        app_sys_set_tx_freq(app_sys_para.tx_freq);
        
        osDelay(100);
    }
}